package com.stylefeng.guns.modular.system.controller;

import com.stylefeng.guns.common.controller.BaseController;
import com.stylefeng.guns.common.persistence.dao.*;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.modular.system.dao.OpUserClassDao;
import com.stylefeng.guns.modular.system.controller.CheckAccessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 用户订阅课程表控制器
 *
 * @author fengshuonan
 * @Date 2018-12-28 10:11:40
 */
@Controller
@RequestMapping("/opUserClass")
public class OpUserClassController extends BaseController {

    private String PREFIX = "/system/opUserClass/";
    @Autowired
    OpUserClassMapper opUserClassMapper;
    @Autowired
    CheckAccessUtil checkAccessUtil;
    @Autowired
    OpUserClassDao opUserClassDao;
    @Autowired
    OrgInfoMapper orgInfoMapper;
    @Autowired
    OrgUserMapper orgUserMapper;
    @Autowired
    OrgClassTypeMapper orgClassTypeMapper;
    /**
     * 跳转到用户订阅课程表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "opUserClass.html";
    }

    /**
     * 跳转到添加用户订阅课程表
     */
    @RequestMapping("/opUserClass_add")
    public String opUserClassAdd() {
        return PREFIX + "opUserClass_add.html";
    }

    /**
     * 跳转到修改用户订阅课程表
     */
    @RequestMapping("/opUserClass_update/{opUserClassId}")
    public String opUserClassUpdate(@PathVariable Integer opUserClassId, Model model) {
        return PREFIX + "opUserClass_edit.html";
    }

    /**
     * 获取用户订阅课程表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        //创建List对象容器
        List<OpUserClass> orgUserClasses = null;
        //获取权限监测返回值
        int infoId =  checkAccessUtil.CheckAccess();
        //  如果返回 -1 表示 userId 不存在 即info_id 不存在
        //  如果返回 0 则表示要查询所有 数据
        //  如果返回 不等于0  则返回值为 info_id
        if(infoId == 0){
            //查询所有对象列表
            orgUserClasses = opUserClassMapper.selectList(null);
        }else if(infoId == -1){
            return null;
        }else{
            //根据infoId 去找到对象信息
            orgUserClasses = opUserClassDao.findByInfoId(infoId);
        }
        for(OpUserClass opUserClass:orgUserClasses){
            OrgInfo orgInfo = orgInfoMapper.selectById(opUserClass.getInfoId());
            OrgUser orgUser = orgUserMapper.selectById(opUserClass.getUserId());
            OrgClassType orgClassType = orgClassTypeMapper.selectById(opUserClass.getClassId());

            Integer b = Integer.valueOf(opUserClass.getClassState());
            if(b == 1){
                opUserClass.setClassState("授课");
            }else if(b == 2){
                opUserClass.setClassState("结课");
            }else if(b == 3){
                opUserClass.setClassState("退课");
            }else {
                opUserClass.setClassState("兔子追月亮了！");
            }

            Integer c =Integer.valueOf(opUserClass.getApplyState());
            if(c == 1){
                opUserClass.setApplyState("意向");
            }else if(c == 2){
                opUserClass.setApplyState("报名成功");
            }else if(c == 3){
                opUserClass.setApplyState("报名中");
            }else {
                opUserClass.setApplyState("兔子追月亮了！");
            }
            opUserClass.setInfoId((orgInfo==null||orgInfo.getOrgName()==null||orgInfo.getOrgName()=="")?"":orgInfo.getOrgName());
            opUserClass.setUserId((orgInfo==null||orgUser.getUserName()==null||orgUser.getUserName()=="")?"":orgUser.getUserName());
            opUserClass.setClassId((orgInfo==null||orgClassType.getClassName()==null||orgClassType.getClassName()==""?"":orgClassType.getClassName()));
        }
//        for(OpUserClass opUserClass:orgUserClasses){
//            String userId = opUserClass
//
//        }
        return orgUserClasses;
    }

    /**
     * 新增用户订阅课程表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add() {
        return super.SUCCESS_TIP;
    }

    /**
     * 删除用户订阅课程表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        return SUCCESS_TIP;
    }


    /**
     * 修改用户订阅课程表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update() {
        return super.SUCCESS_TIP;
    }

    /**
     * 用户订阅课程表详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
